const { Middleware } = require("springbreeze"), //modify 
    Log4js = require("log4js")

module.exports = class LoggerMiddleware extends Middleware {

    constructor() {
        super()
    }

    before() {
        const config = this.config() || {}

        global.LogPath = config.path || process.env.HOME + "/log"
        Log4js.configure({
            appenders: {
                console : {
                    type: "console" ,
                    level : "ALL"
                },
                file : {
                    type: "file",
                    level: 'INFO',
                    filename: global.LogPath + "/out",
                    alwaysIncludePattern: true,
                    daysToKeep: 2,
                    pattern: config.pattern || "yyyy-MM-dd-hh.log",
                    encoding: config.encoding || 'utf-8'
                },
                error : {
                    type: "dateFile",
                    level: 'ERROR',
                    filename: global.LogPath + "/error",
                    alwaysIncludePattern: true,
                    daysToKeep: 2,
                    pattern: config.pattern || "yyyy-MM-dd.log",
                    encoding: config.encoding || 'utf-8'
                }
            },
            categories: {
                default: {
                    appenders: ['console', 'file' ] , level : "ALL"
                },//默认log类型，输出到控制台 log文件 log日期文件 且登记大于info即可
                file: { 
                    appenders: ['file'], level: 'INFO' 
                },  //生产环境 log类型 只输出到按日期命名的文件，且只输出警告以上的log
                console: { 
                    appenders: ['console'], level: 'DEBUG' 
                }, //开发环境  输出到控制台
                debug: { 
                    appenders: ['console'], level: 'DEBUG' 
                }, //调试环境 输出到log文件和控制台    
                error: { 
                    appenders: ['error'], level: 'ERROR' 
                }//error 等级log 单独输出到error文件中 任何环境的errorlog 将都以日期文件单独记录
            },
        })

        //保证同步启动
        global.Log = Log4js.getLogger()
        global.GetLogger = Log4js.getLogger
        global.LogConfigure = Log4js.configure

    }

    async start() {
        
    }

}